﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Recipe
    {
        /// <summary>
        /// 判断菜谱名称是否存在（范围:系统菜谱或者我的菜谱）
        /// 范围有companyId和source决定
        /// </summary>
        /// <example>
        /// 系统菜谱范围条件：companyId=0 and source='1'
        /// 我的菜谱范围条件：companyId=@companyId and source='2'
        /// </example>
        /// <param name="companyId">企业编号</param>
        /// <param name="recipeName">菜谱名称</param>
        /// <param name="source">来源</param>
        /// <returns></returns>
        public static bool isExistsRecipeName(int companyId,String recipeName,String source)
        {
            object tmp = DbHelper.ExecuteScalar("p_Recipe_isExistsName", companyId, recipeName, source);
            if (tmp!=null)
            {
                return Convert.ToBoolean(tmp);
            }
            return false;
        }
        public static int insert(RecipeEntity recipe)
        {
            int id = 0;
            object recipeId = DbHelper.ExecuteScalar("p_Recipe_insert",
                recipe.Name,
                recipe.Rcid,
                recipe.Description,
                recipe.IsShow,
                recipe.Source,
                recipe.CompanyId,
                recipe.Sort,
                recipe.IsRecommend,
                recipe.Favorite
            );

            if (recipeId != null)
            {
                int.TryParse(recipeId.ToString(), out id);
            }

            return id;
        }
        // 前台录入调用方法
        public static int insert(int companyId, int recipeId, string recipeName,int rcid)
        {
            int id = 0;
            
            object tmp = DbHelper.ExecuteScalar("p_Recipe_From_System_To_Diy", companyId, recipeId, recipeName,rcid);
            if (tmp != null)
            {
                int.TryParse(tmp.ToString(), out id);
            }
            return id;
        }
        // 加入我的菜谱
        public static void insert(int companyId, int recipeId)
        {
            DbHelper.ExecuteNonQuery("p_Recipe_Insert_From_System", companyId, recipeId);
        }
        // 将当前菜谱转换成系统菜谱
        public static int insertFromDiyToSystem(RecipeEntity cat)
        {
            int id = 0;

            object tmp = DbHelper.ExecuteScalar("p_Recipe_From_Diy_To_System", cat.RecipeId,cat.Name,cat.Rcid,cat.Description,cat.IsShow,cat.Sort,cat.IsRecommend);
            if (tmp!=null)
            {
                int.TryParse(tmp.ToString(),out id);
            }
            return id;
        }
        public static void update(RecipeEntity recipe)
        {
            DbHelper.ExecuteNonQuery("p_Recipe_update",
                recipe.RecipeId,
                recipe.Name,
                recipe.Rcid,
                recipe.Description,
                recipe.IsShow,
                recipe.Source,
                recipe.CompanyId,
                recipe.Sort,
                recipe.IsRecommend,
                recipe.Favorite
            );
        }

        public static void delete(int recipeId)
        {
            DbHelper.ExecuteNonQuery("p_Recipe_delete", recipeId);
        }

        public static RecipeEntity getById(int recipeId)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_recipe_getById", recipeId);
            RecipeEntity recipe = new RecipeEntity();
            if (dr != null)
            {
                recipe.RecipeId = int.Parse(dr["recipeId"].ToString());
                recipe.Name = dr["name"].ToString();
                recipe.Rcid = int.Parse(dr["rcid"].ToString());
                recipe.Description = dr["description"].ToString();
                recipe.IsShow = bool.Parse(dr["isShow"].ToString());
                recipe.Source = dr["source"].ToString();
                recipe.Sort = int.Parse(dr["sort"].ToString());
                recipe.IsRecommend = bool.Parse(dr["isRecommend"].ToString());
                recipe.Favorite = bool.Parse(dr["favorite"].ToString());
                recipe.CompanyId = int.Parse(dr["companyId"].ToString());
                recipe.InputDt = DateTime.Parse(dr["inputDt"].ToString());
                recipe.CompanyName = dr["companyName"].ToString();
            }
            return recipe;
        }

        /// <summary>
        /// 根据菜谱编号查询菜谱详细信息和菜谱清单
        /// </summary>
        /// <param name="recipeId">菜谱编号</param>
        /// <returns>DataSet.Tables[0] 表示菜谱详细信息；DataSet.Tables[1] 表示菜谱清单</returns>
        public static DataSet getRecipeItemById(int recipeId)
        {
            return DbHelper.ExecuteReDataSet("p_Recipe_listItemById", recipeId);
        }
    }
}